package models.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import models.Serie;

public class DAOSerie implements DAO<Serie>{

	//Inserir Serie
	@Override
	public void inserir(Serie u) {
		try{
			String inserir = "insert into Serie(numero) values(?)";
			PreparedStatement statement = DAOConexao.getConnection().prepareStatement(inserir);
			
			statement.setInt(1, u.numero);
			
			statement.executeUpdate();
			
		}catch(SQLException e){
			e.printStackTrace();
			
		}finally{
			DAOConexao.closeConnection();
		}
		
	}

	//Atualizar o Serie
	@Override
	public void atualizar(Serie u) {
		try{
			String atualizar = "update Serie set numero = ?";
			PreparedStatement statement = DAOConexao.getConnection().prepareStatement(atualizar);
			
			statement.setInt(1, u.numero);
			
			statement.executeUpdate();
			
		}catch(SQLException e){
			e.printStackTrace();
			
		}finally{
			DAOConexao.closeConnection();
		}

	}

	//Deletar o Serie
	@Override
	public void deletar(Serie u) {
		try{
			String deletar = "delete from Serie where numero = ?";
			PreparedStatement statement = DAOConexao.getConnection().prepareStatement(deletar);
			
			statement.setInt(1, u.numero);
			
			statement.executeUpdate();
			 
		}catch(SQLException e){
			e.printStackTrace();
			
		}finally{
			DAOConexao.closeConnection();
			
		}
		
	}

	//Listar
	@Override
	public List<Serie> listar() {
		List<Serie> usu = new ArrayList<Serie>();
		
		try{
			String listar = "select * from Serie";
			PreparedStatement statement = DAOConexao.getConnection().prepareStatement(listar);
			ResultSet result = statement.executeQuery();
			
			
			while(result.next()){
				Serie u = new Serie();
				
				u.numero = result.getInt(1);
				
				usu.add(u);
			}
			
			
		} catch(SQLException e){
			e.printStackTrace();
			
		}finally{
			DAOConexao.closeConnection();
		}
		
		
		
		return usu;
	}
	
}